package com.jeeplus.modules.statistics.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeeplus.database.datasource.annotation.DS;
import com.jeeplus.modules.statistics.mapper.BlacklistMapper;
import com.jeeplus.modules.statistics.model.blacklist.BlacklistModel;
import com.jeeplus.modules.statistics.util.PageBean;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@DS("queuing")
@Service
@RequiredArgsConstructor
@Slf4j
public class BlacklistServic {

   private final BlacklistMapper mapper;
    /**
     * 添加
     * @param blacklistModel 需要添加的对象
     * @return 受影响的行数
     */
    public int addBlacklist(BlacklistModel blacklistModel){
        int i = mapper.addBlacklist(blacklistModel);
        return i;
    }

    /**
     * 逻辑删除（修改该数据的状态进行删除）
     * @param blacklistModel 需要删除的对象
     * @return 受影响的行数
     */
    public int logicDelBlacklist(BlacklistModel blacklistModel){
        int i = mapper.logicDelBlacklist(blacklistModel);
        return i;
    }
    /**
     * 物理删除
     * @param blacklistModel 需要删除的对象
     * @return 受影响的行数
     */
    public int delBlacklist(BlacklistModel blacklistModel){
        int i = mapper.delBlacklist(blacklistModel);

        return i;
    }

    /**
     * 修改
     * @param blacklistModel 需要修改的对象
     * @return
     */
    public int updateBlacklist(BlacklistModel blacklistModel){
        int i = mapper.updateBlacklist(blacklistModel);

        return 0;
    }

    /**
     *查询全部黑名单
     * @param map 分页参数
     * @param blacklistModel 查询参数对象
     * @return
     */
    public PageBean<BlacklistModel> findBlacklist(Map<String,Object> map, BlacklistModel blacklistModel){
        PageBean<BlacklistModel> pageBean=new PageBean<>();
        PageHelper.startPage(Integer.parseInt(map.get("curPage")+""), Integer.parseInt(map.get("pageSize")+""));

        List<BlacklistModel> list=mapper.findBlacklist(blacklistModel);
        PageInfo<BlacklistModel> pageInfo=new PageInfo<>(list);
        pageBean.setCrurentNum(Integer.parseInt(map.get("curPage")+""));
        pageBean.setPageSize(Integer.parseInt(map.get("pageSize")+""));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }

    /**
     * 查询单个黑名单
     * @param blacklistId 黑名单id
     * @return
     */
    public BlacklistModel findBlacklistByid(String blacklistId){
        BlacklistModel blacklistByid = mapper.findBlacklistByid(blacklistId);
        return blacklistByid;
    }

    /**
     * 按身份证查询黑名单
     * @param blacklistCard 黑名单身份证
     * @return
     */
    public BlacklistModel findBlacklistByCard(String blacklistCard){
        BlacklistModel blacklistByCard = mapper.findBlacklistByCard(blacklistCard);

        return blacklistByCard;
    }

    /**
     * 按拉黑结束时间逻辑删除
     * @return
     */
    public int deblocking(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String blacklistEndTime = df.format(new Date());
        int deblocking = mapper.deblocking(blacklistEndTime);
        return deblocking;
    }
}
